import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/http/api/lot_api.dart';
import 'package:xin_nuo_pda/http/api/material_api.dart';
import 'package:xin_nuo_pda/routes/Routes.dart';

import 'AddMaterialState.dart';

class AddMaterialController extends BaseController {
  final AddMaterialState state = AddMaterialState();

  Future<void> getCheckList() async {
    var list = state.bomList.where((element) => element.isSelector??false).toList();
    if(list.isEmpty){
      Fluttertoast.showToast(msg: "请选择物料信息");
      return;
    }
    var materialInfo = await Get.toNamed(Routes.AddMaterialInfo,arguments: {
      "listBean" : list,
      "lotID" : state.lotID
    });
    addMaterialData(materialInfo as List<LotBOMLINELIST>);

  }

  addMaterial() async{
    if(state.materialList.isEmpty){
      Get.snackbar('提示', "请先添加物料信息",
          colorText: Colors.white, backgroundColor: Colors.black87);
      return;
    }
    Map<String, dynamic> params = {'LOTID':state.lotID,'MLOTLIST' : state.materialList};
    var result = await MaterialApi.addMaterial(canelToken,params: params);
    if(result.ok){
      // Get.snackbar('提示', "操作成功",
      //     colorText: Colors.white, backgroundColor: Colors.black87);
      Fluttertoast.showToast(
        msg: "物料添加成功",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
      );
      Get.back();
    }
  }

  getLotInfo() async{
    Map<String, dynamic> params = {'LOTID':state.lotID};
    var result = await LotApi.lotInfo(canelToken,params: params);
    if(result.ok){
      state.lotEntity = result.data;
      update([state.lotEntityId,state.tableControllerId]);
    }
  }

  void addMaterialData(List<LotBOMLINELIST> materialInfo ) {
    state.materialList.addAll(materialInfo);
    update([state.addControllerId]);
  }
}
